// pages/in/in.js
var app = getApp()
import {
  async
} from '../../../utils/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgList: [],
    imgList1: [], //继承
    name: '',
    content: '',
    shiping: [],
    leixing: '',
    video: [],
    modalName: '',
    fixContent: '',
    value: "",
    zhaopian: [],
    wenjian: [],
    flieName: [],
    flieName1: [],
    wenjian1: [],
    s: 0,
    id: '',
    img: []
  },
  onLoad(e) {
    var item = app.globalData.bian
    console.log(item)
    this.setData({
      id: item.id,
      leixing: item.leixing,
      flieName1: item.flieName,
      shiping: item.shiping,
      wenjian1: item.wenjian,
      name: item.name,
      zhaopian: item.zhaopian,
      content: item.content
    })
  },
  changes(e) {
    this.setData({
      value: e.detail.value
    })
  },
  change(e) {
    if (this.data.modalName == '技术名称') {
      this.setData({
        name: this.data.value,
        value: ''
      })
    } else {
      this.setData({
        leixing: this.data.value,
        value: ''
      })
    }
    this.setData({
      modalName: ''
    })
  },
  onTap(e) {
    console.log(e.currentTarget.dataset.id)
    this.setData({
      modalName: e.currentTarget.dataset.id
    })
  },
  in () {
    var that = this
    wx.chooseMessageFile({
      count: 10,
      success: (res) => {
        console.log(res.tempFiles)
        var flieName = that.data.flieName
        var wenjian = that.data.wenjian
        for (var i = 0; i < res.tempFiles.length; i++) {
          flieName.push(res.tempFiles[i].name)
          wenjian.push(res.tempFiles[i].path)
        }
        // that.up(res.tempFiles[0].path,0)
        // that.s()
        this.setData({
          wenjian: wenjian,
          flieName: flieName
        })
      }
    })
  },

  s() {
    let MyFile = new wx.BaaS.File()
    let query = new wx.BaaS.Query()
    query.compare('category_name', '=', "技术文档")
    // query.contains('name', substr)
    MyFile.setQuery(query).find().then(res => {
      // success
      console.log(res) // 10
    }, err => {
      // err
    })
  },
  up(url, k) {
    var that = this

    let MyFile = new wx.BaaS.File()
    let fileParams = {}
    let metaData = {}
    var items
    if (k == 1) {
      items = that.data.shiping
      fileParams = {
        filePath: url,
        fileType: "video"
      }

      metaData = {
        categoryName: '技术视频'
      }
    } else if (k == 2) {
      items = that.data.zhaopian
      fileParams = {
        filePath: url,
        fileType: "image"
      }

      metaData = {
        categoryName: '技术照片'
      }
    } else {
      items = that.data.wenjian1
      fileParams = {
        filePath: url,
        fileType: 'other'
      }

      metaData = {
        categoryName: '技术文档'
      }
    }


    MyFile.upload(fileParams, metaData).then(res => {
      // 上传成功
      let data = res.data // res.data 为 Object 类型
      console.log(data)
      items.push(res.data.path)
      if (k == 1) {
        that.setData({
          shiping: items,
          s: that.data.s + 1
        })
      } else if (k == 2) {
        that.setData({
          zhaopian: items,
          s: that.data.s + 1
        })
      } else {
        that.setData({
          wenjian1: items,
          s: that.data.s + 1
        })
      }

    }, err => {
      console.log(err)
      // HError 对象
    })
  },
  delc1(e) {
    var that = this
    wx.showModal({
      content: '确定要删除？',
      cancelText: '不了',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          that.data.wenjian1.splice(e.currentTarget.dataset.id, 1);
          that.data.flieName.splice(e.currentTarget.dataset.id, 1);
          that.setData({
            wenjian1: that.data.wenjian1,
            flieName: that.data.flieName
          })
        }
      }
    })

  },

  delc(e) {
    var that = this
    wx.showModal({
      content: '确定要删除？',
      cancelText: '不了',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          that.data.wenjian.splice(e.currentTarget.dataset.id, 1);
          that.data.flieName.splice(e.currentTarget.dataset.id, 1);
          that.setData({
            wenjian: that.data.wenjian,
            flieName: that.data.flieName
          })
        }
      }
    })

  },

  ViewImage(e) {
    var items = this.data.imgList
    for (var i = 0; i < this.data.zhaopian.length; i++) {
      items.push(this.data.zhaopian[i])
    }

    wx.previewImage({
      urls: items,
      current: e.currentTarget.dataset.url
    });
  },
  view1(e) {
    var that = this
    wx.navigateTo({
      url: '../../s/s?url=' + that.data.wenjian1[e.currentTarget.dataset.id]
    })
  },

  view(e) {
    var that = this
    wx.navigateTo({
      url: '../../s/s?url=' + that.data.wenjian[e.currentTarget.dataset.id]
    })
  },
  find(url, name) {
    let MyFile = new wx.BaaS.File()
    let query = new wx.BaaS.Query()
    query.compare('category_name', '=', name)
    // query.contains('name', substr)
    MyFile.setQuery(query).find().then(res => {
      // success
      console.log(res.data.objects) // 10
      for (var i = 0; i < res.data.objects.length; i++) {
        if (res.data.objects[i].path == url) {
          console.log(res.data.objects[i].id)
          MyFile.delete(res.data.objects[i].id).then(res => {
            console.log(res)
          })
        }
      }

    }, err => {
      // err
    })
  },
  ups: function () {
    var that = this
    return new Promise((resolve, reject) => {
      for (var i = 0; i < that.data.video.length; i++) {
        that.up(that.data.video[i], 1)
      }
      for (var j = 0; j < that.data.imgList.length; j++) {
        that.up(that.data.imgList[j], 2)
      }
      for (var k = 0; k < that.data.wenjian.length; k++) {
        that.up(that.data.wenjian[k], 0)
      }
      console.log(that.data.s)

      var ui = setInterval(function (e) {
        console.log(that.data.s)
        if (that.data.s == that.data.wenjian.length + that.data.imgList.length + that.data.video.length) {
          resolve(1)
          clearInterval(ui)
        }
      }, 1000)
    })
  },
  from: async function () {
    var that = this
    var i = 0
    wx.showToast({
      title: '上传中...',
      mask: true,
      icon: 'loading'
    })
    var items = this.data.imgList
    for (var i = 0; i < this.data.zhaopian.length; i++) {
      items.push(this.data.zhaopian[i])
    }
    // 向 tableName 为 'product' 的数据表插入一条记录

    var a = await that.ups()
    if (a == 1) {
      // console.log(that.data.wenjian1)

      let tableName = 'jishu'
      let recordID = that.data.id
      let Product = new wx.BaaS.TableObject(tableName)
      let product = Product.getWithoutData(recordID)
      // 设置方式二
      product.set('zhaopian', that.data.zhaopian)
      product.set('shiping', that.data.shiping)
      product.set('content', that.data.content)
      product.set('wenjian', that.data.wenjian1)
      console.log(that.data.shiping)
      product.set('leixing', that.data.leixing)
      product.set('name', that.data.name)
      product.set('flieName', that.data.flieName)
      product.update().then(res => {
        // success

        wx.hideToast({})
        console.log(res)
      }, err => {
        // HError 对象
      })
    }
  },
  /**
   * 选择举报图片
   */
  ChooseImage() {
    wx.chooseImage({
      count: 9, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },

  inshi() {
    var that = this
    wx.chooseMedia({
      count: 4,
      mediaType: ["video"],
      success: res => {
        console.log(res.tempFiles)
        var vi = that.data.video
        for (var i = 0; i < res.tempFiles.length; i++) {
          vi.push(res.tempFiles[i].tempFilePath)
        }
        that.setData({
          video: vi
        })

      }
    })
  },
  delv1(e) {
    var that = this
    wx.showModal({
      content: '确定要删除？',
      cancelText: '不了',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.find(this.data.zhaopian[e.currentTarget.dataset.index], "技术视频")
          this.data.shiping.splice(e.currentTarget.dataset.id, 1);
          this.setData({
            shiping: this.data.shiping
          })
        }
      }
    })
  },
  delv(e) {
    var that = this
    wx.showModal({
      content: '确定要删除？',
      cancelText: '不了',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.video.splice(e.currentTarget.dataset.id, 1);
          this.setData({
            video: this.data.video
          })
        }
      }
    })
  },
  /**
   * 删除举报图片
   * @param {}} e 
   */
  DelImg(e) {
    wx.showModal({
      content: '确定要删除？',
      cancelText: '不了',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },

  /**
   * 删除举报图片
   * @param {}} e 
   */
  DelImg1(e) {
    wx.showModal({
      content: '确定要删除？',
      cancelText: '不了',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.find(this.data.zhaopian[e.currentTarget.dataset.index], "技术照片")
          this.data.zhaopian.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            zhaopian: this.data.zhaopian
          })

        }

      }
    })
  },
  hideModal() {
    this.setData({
      modalName: ''
    })
  },
  setMessage(e) {
    this.setData({
      content: e.detail.value
    })
  },

  setMessage1(e) {
    this.setData({
      fixContent: e.detail.value
    })
  }
})